package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Modelquestion;
import com.teamwork.ZuccEcho.Entity.Moptions;
import com.teamwork.ZuccEcho.Repository.ModelQuestionApiRepository;
import com.teamwork.ZuccEcho.Repository.MoptionsApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/modelquestion")
public class ModelQuestionApiController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private ModelQuestionApiRepository modelquestionApiRepository;
    @Autowired
    private MoptionsApiRepository moptionsApiRepository;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    public List<Modelquestion> modelquestions;//所有模板问题

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseData getModelQuestionList() {
        List<Modelquestion> list = new ArrayList<Modelquestion>(modelquestionApiRepository.findAll());
        return new ResponseData(ExceptionMsg.SUCCESS,list);
    }

    //添加模板题目
    @RequestMapping(value = "/add/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addModelQuestion(@RequestBody Modelquestion modelquestion){

        modelquestionApiRepository.save(modelquestion);
        //获取刚刚添加的模板问题的id
        long mqid = modelquestionApiRepository.findPublicedMqId();
        List<Modelquestion> newlist = modelquestionApiRepository.searchByMQuestionId(mqid);

        //如果不是填空题，就加入输入的选项
        if(modelquestion.getMoptionsList().size()>0){
            List<Moptions> moptions=modelquestion.getMoptionsList();
            for(int i=0;i<moptions.size();i++){
                Moptions moptions1=moptions.get(i);
                moptions1.setModelquestion(newlist.get(0));
                moptionsApiRepository.save(moptions1);
            }
        }

        return new ResponseData(ExceptionMsg.SUCCESS,modelquestion);
    }

    //删除模板题目
    @RequestMapping(value = "/delete/v1/{id}", method = RequestMethod.POST)
    public Response deleteModelQuestion(@PathVariable("id") long id){
        //删除模板问题的选项
        List<Long> optionsid=modelquestionApiRepository.searchAllModelOptionsByQuestionId(id);
        for(int j=0;j<optionsid.size();j++){
            modelquestionApiRepository.deleteOptionByOId(optionsid.get(j));
        }

        modelquestionApiRepository.deleteById(id);
        return result(ExceptionMsg.SUCCESS);
    };

    //修改模板题目
    @RequestMapping(value = "/put/v1/{id}", method = RequestMethod.POST)
    public ResponseData updateModelQuestion(@PathVariable("id") long id, @RequestBody Modelquestion modelquestion){
        modelquestionApiRepository.modifyModelQuestionByMqId(modelquestion.getModel().getM_id(),
                modelquestion.getMq_type(),modelquestion.getMq_name(),modelquestion.isMq_isrequired(),
                modelquestion.getMq_content(),id);
        return new ResponseData(ExceptionMsg.SUCCESS,modelquestion);
    };

    //查询模板题目
    @RequestMapping(value = "/get/v1/{id}", method = RequestMethod.GET)
    public ResponseData findModelQuestion(@PathVariable("id") long id) throws IOException {
        List<Modelquestion> list = new ArrayList<>(modelquestionApiRepository.searchByMQuestionId(id));
        for (Modelquestion c : list) {
            c.getMoptionsList();//懒加载
        }
        String strJSON = JSON.toJSONString(list);//将要返回的列表转为json字符串，返回给前端
        return new ResponseData(ExceptionMsg.SUCCESS,strJSON);

//        Modelquestion modelquestion = modelquestionApiRepository.findById(id);
//        if (modelquestion != null) {
//            return new ResponseData(ExceptionMsg.SUCCESS,modelquestion);
//        }
//        return new ResponseData(ExceptionMsg.FAILED,modelquestion);
    };

    //查
    @RequestMapping(value = "/re/{id}", method = RequestMethod.GET)
    public Modelquestion findModelQuestioned(@PathVariable("id") long id) throws IOException {
        RestTemplate client= restTemplateBuilder.build();
        String uri = "http://localhost:8080/modelquestion/"+id;
        System.out.println(uri);
        Modelquestion modelquestion = client.getForObject (uri,Modelquestion.class,id) ;

        return modelquestion;
    };
}
